/**
 * 新增时间：2009-12-1
 * 朱家进
 * 内容：省级平台切换
 * 
 */
package com.zjht.gmcc.servlet.sys;

import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;
import com.zjht.gmcc.bean.util.InitParam;
import com.zjht.gmcc.bean.util.MyJDBC;

public class OrgaProvEditChannelType extends BaseServlet {

	private static final long serialVersionUID = 1L;

	public boolean process(HttpServletRequest request, HttpServletResponse response, HttpSession session, PrintWriter out, MyJDBC myJDBC, String menu)
			throws Exception {

		int flag = 0;
		String act = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String orgaCode = request.getParameter("orgaCode");
		String channelType = request.getParameter("channelType");
		String orgaName = request.getParameter("orgaName");

		int count = 0;
		boolean error = false;
		String strHallName = "";
		ArrayList arrHallName = new ArrayList();
		String mobileorgacode = null;

		String sql = "SELECT distinct mobileorgacode,(select shortname FROM organiza a WHERE a.orgacode = b.mobileorgacode) shortname  FROM MOBILETOPROVIDER b "
				+ "WHERE providerorgacode=? AND (cityprovidertype=2 or cityprovidertype=3)";
		ps = myJDBC.prepareStatement(sql);
		ps.setString(1, orgaCode);
		rs = ps.executeQuery();
		while (rs.next()) {
			count++;
			error = true;
			strHallName = rs.getString("shortname");
			arrHallName.add(strHallName);
		}

		if (error) {
			act = "仍有" + count + "个厅 供货关系异常";
		}
		if (error) {
			StringBuilder sb = new StringBuilder();
			for (int i = 1; i <= arrHallName.size(); i++) {
				sb.append(" " + arrHallName.get(i - 1) + " ");
				if (i % 5 == 0)
					sb.append("\\n\\n");
			}

			alert(out, act + "\\n\\n：异常厅\\n\\n：<" + sb.toString() + ">\\n\\n的数据提交失败");
			return false;
		}

		if (!error) {

			sql = "SELECT distinct mobileorgacode FROM MOBILETOPROVIDER b WHERE providerorgacode=? AND cityprovidertype=1 ";
			ps = myJDBC.prepareStatement(sql);
			ps.setString(1, orgaCode);
			rs = ps.executeQuery();
			while (rs.next()) {
				// 修改厅
				mobileorgacode = rs.getString("mobileorgacode");
				sql = "UPDATE organiza SET channeltype= ?  WHERE orgacode = ?   ";
				ps = myJDBC.prepareStatement(sql);
				ps.setString(1, channelType);
				ps.setString(2, mobileorgacode);
				ps.executeUpdate();
			}
			// 修改省级平台
			sql = "UPDATE organiza SET channeltype= ?  WHERE orgacode = ?   ";
			ps = myJDBC.prepareStatement(sql);
			ps.setString(1, channelType);
			ps.setString(2, orgaCode);
			flag = ps.executeUpdate();
			// 修改市级平台
			sql = "UPDATE organiza SET channeltype= ?  WHERE parentorgacode = ?   ";
			ps = myJDBC.prepareStatement(sql);
			ps.setString(1, channelType);
			ps.setString(2, orgaCode);
			ps.executeUpdate();

			act = "省平台：" + orgaName + "及以下组织 渠道类型修改";
		}

		if (flag > 0) {
			InitParam.initOrgaInfo();
			alertOK(out, act + "成功！", "dialogArguments.location.href='orga/orga.jsp?orgacode=" + orgaCode + "&channelType=" + channelType
					+ "&orgaType=5';window.returnValue='" + orgaCode + "';window.close();");
		} else {
			errAlert(request, out, menu, act + "失败");
		}
		return flag > 0;
	}
}
